//Created by: Justina Lee

Ext.define('MajorProjectSenchaTouch.view.BookMarkedModule.ResultListItem', {
    extend: 'Ext.dataview.component.DataItem',
    xtype : 'resultListItem',
    alias: 'widget.rlistItem',
    requires:[
    'Ext.Img',
    'Ext.Button',
    'Ext.Panel'
    ],
    config: {
        cls:'result-list-item',
        image:{
            cls:'resultImage',
            docked: 'left'
        },
        title:{
            cls:'resultTitle'
        },
        author:{
            cls:'resultAuthor'
        },
        lineBreak:{
            cls: 'resultLineBreak'
        },
         ebook:{
            pressedCls:'pressed',
            cls: 'btnVisitLink',
            iconCls:'link1',
            iconMask: true,
            text: 'Electronic Version'
        },
        bkFormat:{
            cls:'resultBkFormat'
        },
        disclosure:{
            cls:'resultDisclosure',
            iconCls:'code3',
            iconMask: true,
            ui:'plain'
        },
          locatorButton:{
            cls:'btnLocator',
            pressedCls:'pressed',
            ui: 'plain'
        }, 
        bookMarkedButton:{ 
            pressedCls:'pressed',
            ui: 'plain'
        },
      
        callNumber:{
            cls:'resultCallNumber'
        },
        year:{
            cls:'resultYear'
        },
      
        //--------------------------------------------------------------------------------------------------------------
        dataMap: {            
            getImage: {
                setSrc: 'img'
            },
            getTitle:{
                setHtml: 'bktitle'
            },
            getAuthor:{
                setHtml: 'author'
            },
            getBkFormat:{
                setHtml:'bkformat'
            },
            getCallNumber:{
                setHtml:'callnumber'
            },
            getYear:{
                setHtml:'year'
            }
        }
    },
    
    //apply update methods----------------------------------------------------------------------------------
    applyDisclosure: function(config) {
        return Ext.factory(config, Ext.Button, this.getDisclosure());
    },
    updateDisclosure: function(newDisclosure, oldDisclosure) {
        if (oldDisclosure) {
            this.remove(oldDisclosure);
        }

        if (newDisclosure) {
            this.add(newDisclosure);
        }
    },
    applyBookMarkedButton: function(config) {
        return Ext.factory(config, Ext.Button, this.getBookMarkedButton());
    },
    updateBookMarkedButton: function(newBookMarkedButton, oldBookMarkedButton) {       
        var bmStore = Ext.getStore('BookMarked');
        
        if (oldBookMarkedButton) {
            this.remove(oldBookMarkedButton);
        }

        if (newBookMarkedButton) {            
            newBookMarkedButton.on('tap', this.onBookMarkedButtonTap, this);
            this.add(newBookMarkedButton);            
            
            var record = this.getRecord();
            var index = bmStore.find("docnum", record.get("docnum"));
            
            if(index === -1){
                newBookMarkedButton.setCls('btnAddBookMarked');
            }else{
                newBookMarkedButton.setCls('btnRemoveBookMarked');
            }
        }
    },
    applyLocatorButton: function(config) {
        return Ext.factory(config, Ext.Button, this.getLocatorButton());
    },
    updateLocatorButton: function(newLocatorButton, oldLocatorButton) {
        if (oldLocatorButton) {
            this.remove(oldLocatorButton);
        }

      if (newLocatorButton) {
            newLocatorButton.on('tap', this.onLocatorButtonTap, this);
            this.add(newLocatorButton);
            
            var record = this.getRecord();
            var callnum = record.get('callnumber');
            
            if(callnum ==""){
                newLocatorButton.setCls('btnLocatorDisable');
            }else{
            }
        }
    },
    applyTitle:function(config){
        return Ext.factory(config, Ext.Component, this.getTitle());
    },
    updateTitle: function(newTitle, oldTitle) {
        if (newTitle) {
            this.add(newTitle);
        }

        if (oldTitle) {
            this.remove(oldTitle);
        }
    },
    applyAuthor:function(config){
        return Ext.factory(config, Ext.Component, this.getAuthor());
    },
    updateAuthor: function(newAuthor, oldAuthor) {
        if (newAuthor) {
            this.add(newAuthor);
        }

        if (oldAuthor) {
            this.remove(oldAuthor);
        }
    },
    applyImage: function(config) {
        return Ext.factory(config, Ext.Img, this.getImage());
    }, 
    updateImage: function(newImage, oldImage) {
        if (newImage) {
            this.add(newImage);
        }
 
        if (oldImage) {
            this.remove(oldImage);
        }
    },
    applyBkFormat:function(config){
        return Ext.factory(config, Ext.Component, this.getBkFormat());
    },
    updateBkFormat: function(newBkFormat, oldBkFormat) {
        if (newBkFormat) {
            this.add(newBkFormat);
        }

        if (oldBkFormat) {
            this.remove(oldBkFormat);
        }
    },
    applyCallNumber:function(config){
        return Ext.factory(config, Ext.Component, this.getCallNumber());
    },
    updateCallNumber: function(newCallNumber, oldCallNumber) {
        if (newCallNumber) {
            this.add(newCallNumber);
        }

        if (oldCallNumber) {
            this.remove(oldCallNumber);
        }
    },
    applyYear:function(config){
        return Ext.factory(config, Ext.Component, this.getYear());
    },
    updateYear: function(newYear, oldYear) {
        if (newYear) {
            this.add(newYear);
        }

        if (oldYear) {
            this.remove(oldYear);
        }
    },
    applyEbook:function(config){
        return Ext.factory(config, Ext.Button, this.getEbook());
    },
    updateEbook: function(newEbook, oldEbook) {
        var scope  = this;
        
        if (oldEbook) {
            this.remove(oldEbook);
        }
        
        if (newEbook) {
            newEbook.on('tap', this.onEbookButtonTap, this);
            this.add(newEbook);
            
            var record = this.getRecord();
            var elink = record.get('ebooklink');
            
            if(elink ==""){
                scope.remove(newEbook);
            }else{
            }
        }
    },
    applyLineBreak:function(config){
        return Ext.factory(config, Ext.Panel, this.getLineBreak());
    },
    updateLineBreak: function(newLineBreak, oldLineBreak) {
        if (newLineBreak) {
            this.add(newLineBreak);
        }

        if (oldLineBreak) {
            this.remove(oldLineBreak);
        }
    },
    //Listeners-----------------------------------------------------------------------------------------------
    onBookMarkedButtonTap: function(button, e){
        this.fireEvent("bookMarkedBtnTap", this, button, e);        
    },
    onLocatorButtonTap: function(button, e){
        this.fireEvent("shelfLocatorBtnTap", this, button, e);        
    },
    onEbookButtonTap: function(button, e){
        this.fireEvent("ebookTap", this, button, e);        
    }
});